package net.wds.ssbus.modules.dbconfig.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.wds.ssbus.common.utils.PageUtils;
import net.wds.ssbus.common.utils.Query;
import net.wds.ssbus.modules.dbconfig.dao.TbDbSourceDao;
import net.wds.ssbus.modules.dbconfig.entity.TbDbSourceConfigEntity;
import net.wds.ssbus.modules.dbconfig.entity.TbDbSourceEntity;
import net.wds.ssbus.modules.dbconfig.entity.TbDbSourceTypeEntity;
import net.wds.ssbus.modules.dbconfig.po.DbSourceConfigPo;
import net.wds.ssbus.modules.dbconfig.service.TbDbSourceConfigService;
import net.wds.ssbus.modules.dbconfig.service.TbDbSourceService;
import net.wds.ssbus.modules.dbconfig.service.TbDbSourceTypeService;
import net.wds.ssbus.modules.dbconfig.vo.DbSourceVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("tbDbSourceService")
public class TbDbSourceServiceImpl extends ServiceImpl<TbDbSourceDao, TbDbSourceEntity> implements TbDbSourceService {

    @Autowired
    TbDbSourceTypeService dbSourceTypeService;

    @Autowired
    TbDbSourceConfigService dbSourceConfigService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<TbDbSourceEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper) -> {
                wrapper.like("name", key).or().eq("active", key);
            });
        }
        IPage<TbDbSourceEntity> page = this.page(
                new Query<TbDbSourceEntity>().getPage(params),
                queryWrapper
        );

        IPage<DbSourceVo> voIPage = new Page<>();
        List<TbDbSourceEntity> records = page.getRecords();
        List<DbSourceVo> collect = records.stream().map(item -> {
            DbSourceVo sourceVo = new DbSourceVo();
            BeanUtils.copyProperties(item, sourceVo);
            TbDbSourceTypeEntity typeEntity = dbSourceTypeService.getById(item.getType());
            sourceVo.setDbType(typeEntity.getDbType());
            return sourceVo;
        }).collect(Collectors.toList());
        voIPage.setCurrent(page.getCurrent());
        voIPage.setPages(page.getPages());
        voIPage.setRecords(collect);
        voIPage.setSize(page.getSize());
        voIPage.setTotal(page.getTotal());
        return new PageUtils(voIPage);
    }

    @Transactional
    @Override
    public void saveSource(DbSourceConfigPo dbSourceConfigPo) {
//        System.out.println(dbSourceConfigPo.getConfigItem().toString());
        TbDbSourceEntity tbDbSourceEntity = new TbDbSourceEntity();
        BeanUtils.copyProperties(dbSourceConfigPo, tbDbSourceEntity);
        tbDbSourceEntity.setActive(0);
        this.save(tbDbSourceEntity);
        String string = dbSourceConfigPo.getConfigItem().toString();
        String replace = string.replace(" ", "");
        String substring = replace.substring(1, replace.length() - 1);
        String[] split = substring.split(",");
        List<TbDbSourceConfigEntity> list = new ArrayList<>();
        for (String s : split) {
            TbDbSourceConfigEntity sourceConfigEntity = new TbDbSourceConfigEntity();
            sourceConfigEntity.setDbSourceId(tbDbSourceEntity.getId());
            String[] split1 = s.split("=");
            sourceConfigEntity.setConfType(split1[0]);
            sourceConfigEntity.setConfValue(split1[1]);
            list.add(sourceConfigEntity);
        }

        dbSourceConfigService.saveBatch(list);
//        System.out.println(configItem.getClass().getDeclaredFields().length);
    }

    @Transactional
    @Override
    public void updateSourceById(DbSourceConfigPo dbSourceConfigPo) {
        TbDbSourceEntity tbDbSourceEntity = new TbDbSourceEntity();
        BeanUtils.copyProperties(dbSourceConfigPo, tbDbSourceEntity);
        this.updateById(tbDbSourceEntity);
        String string = dbSourceConfigPo.getConfigItem().toString();
        String replace = string.replace(" ", "");
        String substring = replace.substring(1, replace.length() - 1);
        String[] split = substring.split(",");
        List<TbDbSourceConfigEntity> addlist = new ArrayList<>();
        List<TbDbSourceConfigEntity> uplist = new ArrayList<>();
        for (String s : split) {
            String[] strings = s.split("=");
            TbDbSourceConfigEntity sourceConfigEntity = dbSourceConfigService.getOne(new QueryWrapper<TbDbSourceConfigEntity>().eq("conf_type", strings[0]).eq("db_source_id", dbSourceConfigPo.getId()));
             if (sourceConfigEntity == null){
                TbDbSourceConfigEntity configEntity = new TbDbSourceConfigEntity();
                configEntity.setConfType(strings[0]);
                configEntity.setConfValue(strings[1]);
                configEntity.setDbSourceId(dbSourceConfigPo.getId());
                addlist.add(configEntity);
            }else {
                sourceConfigEntity.setConfValue(strings[1]);
                uplist.add(sourceConfigEntity);
            }
        }
        dbSourceConfigService.saveBatch(addlist);
        dbSourceConfigService.updateBatchById(uplist);
    }
}